import { request } from '@/api/request';
import { ACOUNTAPIPREFIX, APIUNICRON, BASEAUTHTOKENFROMSBT, BASETOKENFROMSBT } from '@/common';
import { store } from '@/redux/store';
import React, { Ref } from 'react';

export type selectListObj = {
  [key: string]: any;
};

export interface AllStaffListObj {
  org: selectListObj[];
  role: selectListObj[];
  job: selectListObj[];
  isTimebtn?: any;
}
export interface ListProps {
  innerRef: React.MutableRefObject<any>;
  count: number;
  searchVal: string;
  defaultValue?: any[];
  setCount(value: number): void;
  onChange?(value: { type: string; item: selectListObj }): void;
}

export type Changeobj = {
  list: selectListObj[];
  groupList: AllStaffListObj;
};

export interface TreePopProps {
  innerRef: Ref<{ showModal: () => void } | undefined>;
  defaultValue?: AllStaffListObj;
  isTimebtn?: any;
  setSubmitload?: any;
  // 备注:temp {}
  onChange(value: Changeobj): void;
  getuseID?: any;
}
export type StaffObj = {
  name: string;
  guid: string;
  [key: string]: string | number;
};
export type RoleObj = {
  name: string;
  groupId: string;
  [key: string]: string | number;
};
export type JobObj = {
  name: string;
  groupId: string;
  [key: string]: string | number;
};

export type orgObj = {
  parentId: string;
  name: string;
  guid: string;
  [key: string]: string;
};
export type partsObj = {
  lowerLevel?: number;
  guid: string;
  name: string;
  [key: string]: any;
};
export type partsPObj = {
  personId: string;
  name: string;
  [key: string]: string;
};

export type orgListObj = {
  type: number;
  // partsList: partsObj[] | partsPObj[]
  partsList: any[];
};

export type lvobj = {
  name: string;
  guid: string;
  [key: string]: string;
};
export type lvRoleobj = {
  roleName: string;
  roleId: string;
  [key: string]: string;
};
export type OrgDataObj = {
  type: number;
  partsList: partsObj[];
};

// 获取组织
export type SearchData = {
  entId: string;
  keyWord?: string;
  departId?: string;
};

export async function getOrgList(params: SearchData) {
  const { data } = await request<OrgDataObj[]>({
    method: 'post',
    url: APIUNICRON + '/ent/Im/api/entPersonnel/noticeSearch.htm',
    data: params,
    headers: {
      ClientType: 'WebEnt',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
    noAutoShowErrorMessage: true,
  });

  return data;
}

// 获取角色
export type roleList = {
  roleId: string;
  roleName: string;
  [key: string]: any;
};
export type pList = {
  personId: string;
  persongName: string;
  [key: string]: any;
};
export type RoleGroup = {
  groupId: string;
  name: string;
  isDefault: number;
  rlist: roleList[];
  [key: string]: any;
};
export type RoleDataObj = {
  roleGroupList?: RoleGroup[];
  plist?: pList[];
};
type SearchRoleData = {
  entId: string;
  keyWord?: string;
  roleId?: string;
};

export async function getRoleByGroup(params: SearchRoleData) {
  const { data } = await request<RoleDataObj[]>({
    method: 'post',
    url: APIUNICRON + '/ent/Im/api/entPersonnel/noticeSearch.htm',
    params,
    noAutoShowErrorMessage: true,
    headers: {
      ClientType: 'WebEnt',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
  });

  return data;
}

// 接口名称: 按组织架构列表、员工ID列表、角色列表、岗位列表企业员工列表
// async function getMixPersonList(searchData: any) {
//   const { data } = await postJson<roleDataObj>(
//     "/unicron/ent/Im/api/entPersonnel/getMixPersonList",
//     searchData
//   )
//   return data
// }

// 获取岗位
export type JobList = {
  guid: string;
  name: string;
  [key: string]: any;
};
export type jpList = {
  personId: string;
  persongName: string;
  [key: string]: any;
};
export type JobGroup = {
  groupId: string;
  name: string;
  isDefault: number;
  plist: JobList[];
};
export type JobDataObj = {
  positionGroupList?: JobGroup[];
  plist?: jpList[];
};
type SearchJobData = {
  entId: string;
  keyWord?: string;
  positionId?: string;
};

export async function getJobByGroup(params: SearchJobData) {
  const { data } = await request<JobDataObj[]>({
    method: 'post',
    url: ACOUNTAPIPREFIX + '/ent/api/position/positionNoticeList',
    params,
    headers: {
      ClientType: 'WebEnt',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
    noAutoShowErrorMessage: true,
  });

  return data;
}

type PositionGroupEntity = {
  groupId: string;
  name: string;
  isDefault: number;
  state: number;
};

type PositionEntity = {
  guid: string;
  name: string;
  /**
   * @desc 0启用中 1停用中
   */
  jobState: number;
  code: string;
  /**
   * @desc 人数
   */
  peopleNumber: number;
};

type GetPositionByGroupResponseData = (PositionGroupEntity & {
  plist: PositionEntity[];
})[];

// 获取分组形势的岗位数据
// function getPositionByGroup(params?: { entId?: string; state?: number }) {
// return postParam<GetPositionByGroupResponseData>(
//   "/account/ent/api/position/newPositionList",
//   { entId: store.getState().loginEnt.entId, ...params }
// ).then((v) => v.data)
// }

export async function getPositionByGroup(params: { entId?: string; state?: number }) {
  const { data } = await request<JobDataObj[]>({
    method: 'post',
    url: ACOUNTAPIPREFIX + '/ent/api/position/newPositionList',
    params,
    noAutoShowErrorMessage: true,
  });

  return data;
}

export type PersonData = {
  entityId: string;
  personId: string;
  personName: string;
  personLogo?: string;
  realName?: string;
  personPhone?: string;
  entId: string;
};

// 按组织架构列表和员工ID列表获取企业员工列表
export async function getPersonList(params: { orgGuidList?: string[]; personIdList?: string[] }) {
  const { data } = await request<PersonData[]>({
    method: 'post',
    url: APIUNICRON + '/ent/Im/api/entPersonnel/getPersonList',
    data: params,
    noAutoShowErrorMessage: true,
    headers: {
      'Content-Type': 'application/json; charset=UTF-8',
      ClientType: 'WebEnt',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
  });

  return data;
}
